﻿/// <reference path="/Scripts/jquery-1.9.0.min.js">
///<reference path="/Scripts/jQuery.tmpl.min.js"></script>
var pageHandler = function (page_option, urldata) {

    this.pageIndex = 0;
    this.option = page_option;
    this.urldata = urldata;
    var self = this;
    $("#" + this.option.pageID).delegate(".divPreviousPage", "click", function () {
        if ($(this).hasClass("button_nenable")) {
            return false;
        }
        else {
            self.pageIndex -= 1;
            self.dataLoad(self.pageIndex);
        }
    });
    $("#" + this.option.pageID).delegate(".divNextPage", "click", function () {
        if ($(this).hasClass("button_nenable")) {
            return false;
        }
        else {
            self.pageIndex += 1;
            self.dataLoad(self.pageIndex);
        }
    });
    $("#" + this.option.pageID).delegate(".btnGoPageIndex", "click", function () {
        var inputPageIndex = parseInt($("#" + self.option.pageID + " .txtPageIndexInput input").val());
        self.pageIndex = inputPageIndex - 1;
        self.dataLoad(self.pageIndex);
    });
    $("#" + this.option.pageID).delegate(".txtPageIndexInput input", "keypress", function () {
        var code = event.keyCode;
        if (code < 48 || code > 57) {
            return false;
        }
    });
    $("#" + this.option.pageID).delegate(".pageindex_list", "click", function () {
        var c_index = parseInt($(this).attr("data-index"));
        self.pageIndex = c_index;
        self.dataLoad(self.pageIndex);
    });
}
pageHandler.prototype = {
    pageInit: function () {
        this.pageIndex = 0;
        this.dataLoad(0);
    },
    dataLoad: function (index) {
        var s = this.option;
        var self = this;
        this.urldata["pageIndex"] = index;
        $.getJSON(this.option.url, this.urldata, function (data) {
            $("#" + s.contentID).html("");
            if (data.ItemList.length > 0) {
                $("#" + s.tmplID).tmpl(data.ItemList).appendTo("#" + s.contentID);
            }
            if (data.TotalPages > 1 && data.ItemList.length > 0) {
                $("#" + self.option.pageID).html('<div style="height:20px;clear:both;">'
                                                    + '<div  class="divPreviousPage button_enable">上页</div>'
                                                    + '<div  class="divPageDesc" style="float:left; height:20px;text-align:center;"></div>'
                                                    + '<div  class="divNextPage button_enable" >下页</div><div class="txtPageIndexInput"><input type="text"/>'
                                                    + '</div><div class="btnGoPageIndex">Go</div></div>');
                var divPreviousPage = $("#" + s.pageID + " .divPreviousPage");
                var divNextPage = $("#" + s.pageID + " .divNextPage");
                var divPageDesc = $("#" + s.pageID + " .divPageDesc");
                var sIndex = 0;
                var eIndex = 0;
                var strpagehtml = "";
                if (index - 4 > 0) {
                    sIndex = index - 4;
                }
                else {
                    sIndex = 0;
                }
                eIndex = sIndex + 9;
                if (eIndex > (data.TotalPages - 1)) {
                    eIndex = data.TotalPages - 1
                    sIndex = eIndex - 9 > 0 ? eIndex - 9 : 0;
                }

                if (sIndex > 0) {
                    strpagehtml += '<div class="pageindex_list"  data-index=0>1</div><div class="pageindex_mark">...</div>'
                }
                for (var i = sIndex; i <= eIndex; i++) {
                    strpagehtml += '<div class="pageindex_list"  data-index=' + i + '>' + (i + 1) + '</div>';
                }
                if (eIndex < (data.TotalPages - 1)) {
                    strpagehtml += '<div class="pageindex_mark">...</div><div class="pageindex_list"  data-index=' + (data.TotalPages - 1) + '>' + data.TotalPages + '</div>'
                }
                divPageDesc.html(strpagehtml);
                $("#" + self.option.pageID + " .pageindex_list").removeClass("current_pageindex");
                $("#" + self.option.pageID + " .pageindex_list[data-index=" + index + "]").addClass("current_pageindex");
                //divPageDesc.text("第" + (index + 1).toString() + "页" + ",共" + data.TotalPages + "页");

                if (data.HasPreviousPage && data.HasNextPage) {
                    divPreviousPage.removeClass("button_nenable");
                    divPreviousPage.addClass("button_enable")
                    divNextPage.removeClass("button_nenable");
                    divNextPage.addClass("button_enable");
                }
                else if (!data.HasPreviousPage && data.HasNextPage) {
                    divPreviousPage.removeClass("button_enable");
                    divPreviousPage.addClass("button_nenable");
                    divNextPage.removeClass("button_nenable");
                    divNextPage.addClass("button_enable");
                }
                else if (data.HasPreviousPage && !data.HasNextPage) {
                    divPreviousPage.removeClass("button_nenable");
                    divPreviousPage.addClass("button_enable")
                    divNextPage.removeClass("button_enable");
                    divNextPage.addClass("button_nenable");
                }
                else {
                    divPreviousPage.removeClass("button_enable");
                    divPreviousPage.addClass("button_nenable")
                    divNextPage.removeClass("button_enable");
                    divNextPage.addClass("button_nenable");
                }
            }
            else {
                $("#" + self.option.pageID).html("");
            }
        });
    }
}